package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.bac.BacAntClassDto;
import com.ysd.lis.entity.bac.*;
import com.ysd.lis.entity.lab.LabSampleType;
import com.ysd.lis.mapper.bac.BacDrugNotReportMapper;
import com.ysd.lis.request.bac.BacAntClassParam;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.bac.BacDrugNotReportService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 微生物基础字典 服务实现类
 * </p>
 *
 * @author HuaiXing
 * @since 2025-03-20
 */
@Service
public class BacDrugNotReportServiceImpl extends ServiceImpl<BacDrugNotReportMapper, BacDrugNotReport> implements BacDrugNotReportService {

    @Autowired
    BacDrugNotReportMapper bacDrugNotReportMapper;

    @Autowired
    SysBasDictDetailService sysBasDictDetailService;

    @Override
    public Result findBacDrugNotReport(BacAntClassParam param) {
        MPJLambdaWrapper<BacDrugNotReport> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacDrugNotReport.class)
                .selectAs(LabSampleType::getSampleTypeName, BacDrugNotReport::getSampleTypeName)
                .selectAs(BacGermDict::getGermNa, BacDrugNotReport::getGermNa)
                .selectAs(BacAntibioticsDict::getAntName, BacDrugNotReport::getDrugNa)

                .leftJoin(LabSampleType.class, "sa", p -> p.eq(LabSampleType::getSampleTypeCode, BacDrugNotReport::getSampleType).eq(LabSampleType::getDelFlag, 0))
                .leftJoin(BacGermDict.class, "bg", p -> p.eq(BacGermDict::getGermNo, BacDrugNotReport::getGermNo).eq(BacGermDict::getDelFlag, 0))
                .leftJoin(BacAntibioticsDict.class, "bd", p -> p.eq(BacAntibioticsDict::getAntCode, BacDrugNotReport::getDrugNo).eq(BacAntibioticsDict::getDelFlag, 0));


        queryWrapper.eq(BacDrugNotReport::getDelFlag, 0)
                .orderByAsc(BacDrugNotReport::getCreateTime);
        List<BacDrugNotReport> bacDrugNotReports = bacDrugNotReportMapper.selectList(queryWrapper);


        List<Map<String, Object>> genusList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("genus").getData();

        for (BacDrugNotReport bacDrugNotReport : bacDrugNotReports){
            if (StringUtils.isNotBlank(bacDrugNotReport.getGenus()) && CollectionUtils.isNotEmpty(genusList)) {
                genusList.stream().filter(a -> a.get("code").equals(bacDrugNotReport.getGenus())).findFirst().ifPresent(stringObjectMap -> bacDrugNotReport.setGenusNa((String) stringObjectMap.get("name")));
            }
        }

        return Result.succ(1, "查询成功", bacDrugNotReports);

    }

    @Override
    public Result saveOrUpdateBacDrugNotReport(BacAntClassDto dto) {
        List<BacDrugNotReport> bacDrugNotReportList = dto.getBacDrugNotReportList();
        boolean b = this.saveOrUpdateBatch(bacDrugNotReportList);
        if(b){
            return Result.succ(1, "成功", null);
        }else {
            return Result.fail(400,"失败","");
        }
    }

    @Override
    public Result batchDeleteBacDrugNotReport(BacAntClassDto dto) {
        List<BacDrugNotReport> bacDrugNotReportList = dto.getBacDrugNotReportList();
        List<String> ids = bacDrugNotReportList.stream().map(BacDrugNotReport::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "成功", null);
        }else {
            return Result.fail(400,"失败","");
        }
    }
}
